package com.angel.controller;

import com.angel.bean.SysMenu;
import com.angel.service.OtherlogService;
import com.angel.service.SysMenuService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/m")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private OtherlogService otherlogService;

    @RequestMapping("/menu")
    public String getMenu(Model model) {
        SysMenu sysMenu = new SysMenu();
        sysMenu.setParentId("0");
        List<SysMenu> sysMenus = sysMenuService.findAll(sysMenu);
        model.addAttribute("top", sysMenus);
        return "menuManager";
    }

    /**
     * 获取子菜单
     *
     * @param partid
     * @return
     */

    @RequestMapping("/getchild")
    @ResponseBody
    public Object getChildMenu(@Param("partid") String partid) {

        try {
            Integer.valueOf(partid);
        } catch (Exception e) {
            return null;
        }
        SysMenu sysMenu = new SysMenu();
        sysMenu.setParentId(partid);
        List<SysMenu> sysMenus = sysMenuService.findAll(sysMenu);
        if (sysMenus == null || sysMenus.isEmpty()) {
            return null;
        }
        return sysMenus;
    }


    @RequestMapping("/getOne")
    @ResponseBody
    public Object getOneMenu(@Param("id") String id) {
        int mid = 1;
        try {
            mid = Integer.valueOf(id);
        } catch (Exception e) {
            return null;
        }
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(mid);
        sysMenu = sysMenuService.findOne(sysMenu);
        return sysMenu;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object updateMenu(SysMenu sysMenu) {
        Map<String, String> map = new HashMap<>();
        if (sysMenu.getId() == null) {
            return null;
        }
        int flag =sysMenuService.update(sysMenu);
        if (flag==0){
            map.put("data","数据异常");
        }else {
            map.put("data","更新成功");
        }
        return map;
    }
}
